﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.Product;

namespace FisheryPlatform.Web.WWW.Product
{
    public partial class Index : System.Web.UI.Page
    {
        protected List<ProductEntity> ProList = null;
        protected int pageIndex = 0;
        protected int pageSize = 30;
        protected int pageCount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            #region
            if (!int.TryParse(Request.QueryString["pageIndex"], out pageIndex))
            {
                pageIndex = 1;
            }

            pl_Area.SelectedItemIds = Request.QueryString["areaId"];
            #endregion
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            #region
            var sqlWhereString = "";
            GetSearchCondition(out sqlWhereString);
            ProList = ProductManager.Search(sqlWhereString, pageIndex, pageSize, out pageCount);

            this.pb_PageBar.PbModel.CurrentIndex = pageIndex;
            this.pb_PageBar.PbModel.PageCount = pageCount;
            this.pb_PageBar.PbModel.PageSize = pageSize;
            this.pb_PageBar.PbModel.ChangePageUrl = "?" + this.pb_PageBar.PbModel.GetUrlParams(Request);
            this.pb_PageBar.PbModel.IsForAjax = true;
            this.pb_PageBar.PbModel.AjaxPageClickCallback = "PageChanged({index})";
            #endregion
        }

        private void GetSearchCondition(out string sqlWhereString)
        {
            #region
            sqlWhereString = " 1=1 ";

            if (string.IsNullOrEmpty(Request.QueryString["t"]) == false && Request.QueryString["t"] != "0")
            {
                sqlWhereString += " and type=" + Request.QueryString["t"];
            }

            DateTime dateStart = DateTime.Now;
            if (DateTime.TryParse(Request.QueryString["DateStart"], out dateStart))
            {
                sqlWhereString += " and CreateDate>='" + dateStart.ToString("yyyy-MM-dd") + "'";
            }

            DateTime dateEnd = DateTime.Now;
            if (DateTime.TryParse(Request.QueryString["DateEnd"], out dateEnd))
            {
                sqlWhereString += " and CreateDate<='" + dateEnd.ToString("yyyy-MM-dd") + "'";
            }

            decimal minPrice = 0, maxPrice = 0;
            if (decimal.TryParse(Request.QueryString["minPrice"], out minPrice) && minPrice > 0)
            {
                sqlWhereString += " and MinPrice >=" + Request.QueryString["minPrice"].Trim();
            }

            if (decimal.TryParse(Request.QueryString["maxPrice"], out maxPrice) && maxPrice > 0 && maxPrice >= minPrice)
            {
                sqlWhereString += " and MaxPrice <=" + Request.QueryString["maxPrice"].Trim();
            }

            long areaId = 0;
            if (long.TryParse(Request.QueryString["areaId"], out areaId))
            {
                sqlWhereString += " and AreaId=" + Request.QueryString["areaId"];
            }

            #endregion
        }
    }
}